package com.ebelter.btcomlib.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ebelter.btcomlib.models.bluetooth.bean.SleepQuality;
import com.ebelter.btcomlib.models.bluetooth.bean.SleepUnit;

import java.util.ArrayList;
import java.util.List;


public class PieBar extends View {



    public PieBar(Context context) {
        this(context,null);
    }

    public PieBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PieBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private Paint paint;

    private int width;
    private int height;


    private List<SleepUnit> datas;

    private int shenColor;
    private int qianColor;
    private int shiColor;
    /**
     * 初始化
     */
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        datas=new ArrayList<>();
        shenColor= Color.parseColor("#25d875");
        qianColor= Color.parseColor("#fbc63f");
        shiColor= Color.parseColor("#ff5692");
    }






    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width=getMeasuredWidth();
        height=getMeasuredHeight();
    }

    public void addOneUnit(SleepUnit sleepUnit){
        if (sleepUnit==null){
            return;
        }
        datas.add(sleepUnit);
        calculate();
        invalidate();
    }

    public void setDatas(List<SleepUnit> datas){
        this.datas=datas;
        calculate();
        invalidate();
    }



    public long timeTotal;
    private void calculate() {
        long total = 0;
        for(SleepUnit sleepUnit:datas){
            total+=sleepUnit.duration;
        }
        timeTotal=total;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (timeTotal>0){
            float leftFrom = 0;
            for(SleepUnit sleepUnit:datas){
                sleepUnit.percentage = sleepUnit.duration*1.0 / timeTotal;

                float left = leftFrom;

                float right = (float) (leftFrom +sleepUnit.percentage*width);

                float top  = 0;


                if (sleepUnit.sleepQuality== SleepQuality.DeepSleep){
                    paint.setColor(shenColor);
                    top=height*0.45f;
                }else if (sleepUnit.sleepQuality== SleepQuality.ShallowSleep){
                    paint.setColor(qianColor);
                    top=height*0.3f;
                }else if (sleepUnit.sleepQuality== SleepQuality.Sleeplessness){
                    paint.setColor(shiColor);
                    top=0;
                }



                canvas.drawRect(left,top,right,height,paint);

                leftFrom=right;

            }
        }



    }


}
